# Contributor: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
# Maintainer: Bart Ribbers <bribbers@disroot.org>
pkgname=opencv
pkgver=4.6.0
pkgrel=0
pkgdesc="An open source computer vision and machine learning library"
url="https://opencv.org/"
# s390x blocked by vtk -> netcdf
# ppc64le: ftbfs alpine/aports#13329
arch="all !s390x !ppc64le"
license="BSD-3-Clause"
subpackages="$pkgname-dbg $pkgname-dev py3-$pkgname:_py3"
depends_dev="
	blas-dev
	eigen-dev
	ffmpeg-dev
	freetype-dev
	glew-dev
	gstreamer-dev
	harfbuzz-dev
	hdf5-dev
	lapack-dev
	libdc1394-dev
	libgphoto2-dev
	libtbb-dev
	mesa-dev
	openexr-dev
	openjpeg-dev
	openjpeg-tools
	py3-setuptools
	qt5-qtbase-dev
	vtk-dev
	"
makedepends="$depends_dev
	cmake
	samurai
	python3-dev
	py3-numpy-dev
	"
source="https://github.com/opencv/opencv/archive/$pkgver/opencv-$pkgver.tar.gz
	https://github.com/opencv/opencv_contrib/archive/$pkgver/opencv_contrib-$pkgver.tar.gz
	"
# Tests require human interaction
# net required to download a data file (face_landmark_model.dat)
options="net !check"

case "$CARCH" in
	x86_64)
		_extra_cmake_flags="-DENABLE_BUILD_HARDENING=ON -DCPU_BASELINE_DISABLE=SSE3 -DCPU_BASELINE_REQUIRE=SSE2"
		;;
	*)
		_extra_cmake_flags="-DENABLE_BUILD_HARDENING=ON"
esac

prepare() {
	default_prepare

	cd "$srcdir/opencv_contrib-$pkgver"/modules
	# Only use modules that we care about
	mv \
		aruco \
		face \
		tracking \
		optflow \
		plot \
		shape \
		superres \
		videostab \
		ximgproc \
		"$builddir"/modules/
}

build() {
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DENABLE_LTO=ON \
		-DENABLE_PGO=ON \
		-DWITH_OPENCL=ON \
		-DWITH_OPENGL=ON \
		-DWITH_TBB=ON \
		-DWITH_VULKAN=ON \
		-DWITH_QT=ON \
		-DWITH_ADE=OFF \
		-DWITH_opencv_gapi=OFF \
		-DWITH_IPP=OFF \
		-DBUILD_WITH_DEBUG_INFO=ON \
		-DBUILD_TESTS=OFF \
		-DBUILD_PERF_TESTS=OFF \
		-DBUILD_EXAMPLES=OFF \
		-DINSTALL_C_EXAMPLES=OFF \
		-DINSTALL_PYTHON_EXAMPLES=OFF \
		-DOPENCV_SKIP_PYTHON_LOADER=ON \
		-DOPENCV_GENERATE_PKGCONFIG=ON \
		-DOPENCV_ENABLE_NONFREE=OFF \
		-DOPENCV_GENERATE_SETUPVARS=OFF \
		-DEIGEN_INCLUDE_PATH=/usr/include/eigen3 \
		-DLAPACK_LIBRARIES="/usr/lib/liblapack.so;/usr/lib/libblas.so;/usr/lib/libcblas.so" \
		-DCMAKE_SKIP_INSTALL_RPATH=ON \
		-DPYTHON_EXECUTABLE=/usr/bin/python3 \
		$_extra_cmake_flags
	cmake --build build
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

samples() {
	pkgdesc="OpenCV Samples"
	depends="$pkgname=$pkgver"
	mkdir -p "$subpkgdir/usr/share/opencv4"
	mv "$pkgdir/usr/share/opencv4/samples" "$subpkgdir/usr/share/opencv4"
}

_py3() {
	pkgdesc="$pkgdesc (python modules)"
	depends="py3-numpy"

	amove usr/lib/python3.*
}

sha512sums="
93d7807794682990b6a1d1de1851986ae3c5d1afe6605f3f8cace03ba5e3390bee2568bc0f335af34d3fc974df64cbce0ce685261ec2abd693d259b97b15bc46  opencv-4.6.0.tar.gz
2e9cc9632774babf59cd186cd7b7edbd35a816bdda2acb51339c514a33fc6d8c3f1687eb3b0f6827304e3fcb0f9f3e81d47e8ab08239175750ac1240cc99dc5d  opencv_contrib-4.6.0.tar.gz
"
